package com.hc.markbook.action;

import com.hc.markbook.data.DataCenter;
import com.hc.markbook.dialog.AddNoteDialog;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;

/**
 * 选中文件内容，右键popup下首个action
 */
public class PopupAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // 获取选中的内容
        Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
        SelectionModel selectionModel = editor.getSelectionModel();
        String selectedText = selectionModel.getSelectedText();
        DataCenter.SELECT_TEXT = selectedText;
        // 获取当前文件名
        String name = e.getRequiredData(CommonDataKeys.PSI_FILE)
                .getViewProvider()
                .getVirtualFile() // idea会为每个文件创建一个虚拟文件
                .getName();
        System.out.println("path: " + e.getRequiredData(CommonDataKeys.PSI_FILE)
                .getViewProvider()
                .getVirtualFile().getPath());
        DataCenter.FILE_NAME = name;

        // 弹框：添加笔记
        AddNoteDialog addNoteDialog = new AddNoteDialog();
        addNoteDialog.show();
        System.out.println("添加笔记: " + selectedText);
    }
}
